const cloud = require('wx-server-sdk')

// 初始化 cloud
cloud.init({
    // API 调用都保持和云函数当前所在环境一致
    env: cloud.DYNAMIC_CURRENT_ENV
})
const db = cloud.database();
const _ = db.command;
/**
 * 这个示例将经自动鉴权过的小程序用户 openid 返回给小程序端
 * 
 * event 参数包含小程序端调用传入的 data
 * 
 */
exports.main = (event, context) => {
    const response = {};
    const { OPENID } = cloud.getWXContext();
    return new Promise((resolve, reject) => {
        const query = {
            openid: _.eq(OPENID)
        };
        db.collection('dakaRecords')
            .field({
                createdDay: true
            })
            .get()
            .then((res) => {
                if (res.data.length) {
                    response.code = 200;
                    response.body = [...new Set(res.data.map(item => item.createdDay))];
                    resolve(response);
                } else { //用户未注册
                    response.code = 200;
                    response.body = null;
                    resolve(response);
                }
            })
            .catch(err => {
                response.code = 500;
                response.body = err;
            })
            // 获取 WX Context (微信调用上下文)，包括 OPENID、APPID、及 UNIONID（需满足 UNIONID 获取条件）等信息
    })
}